function insert_text(open, close)
{

	msgfield = (document.all) ? document.all.req_message : ((document.getElementById('afocus') != null) ? (document.getElementById('afocus').req_message) : (document.getElementsByName('req_message')[0]));
	msgfield.focus();
	// IE support
	if (document.selection && document.selection.createRange)
	{
		sel = document.selection.createRange();
		sel.text = open + sel.text + close;
		msgfield.focus();
	}
	// Moz support
	else if (msgfield.selectionStart || msgfield.selectionStart == '0')
	{
		var startPos = msgfield.selectionStart;
		var endPos = msgfield.selectionEnd;
		msgfield.value = msgfield.value.substring(0, startPos) + open + msgfield.value.substring(startPos, endPos) + close + msgfield.value.substring(endPos, msgfield.value.length);
		if (startPos == endPos && open == '')
		{
			msgfield.selectionStart = startPos + close.length;
			msgfield.selectionEnd = endPos + close.length;
		}
		else
		{
			msgfield.selectionStart = startPos + open.length;
			msgfield.selectionEnd = endPos + open.length;
		}
		msgfield.focus();
	}
	// Fallback support for other browsers
	else
	{
		msgfield.value += open + close;
		msgfield.focus();
	}
	return false;
}

function get_quote_text()
{
	//IE
	if (document.selection && document.selection.createRange()) {quote_text = document.selection.createRange().text;}
	//NS,FF,SM
	if (document.getSelection) {quote_text = document.getSelection();}
}

function Quote(user_name, mmid)
{
	if (quote_text!='')
	{
		var endq = '[quote=' + user_name + ']\n' + quote_text + '\n[/quote]\n';
		insert_text('',endq);
	}
	else if (typeof(jQuery) != 'undefined' && mmid != 'undefined' && typeof(mmid) == 'number' && mmid > 0)
	{
		if (apq_id != -1)
		{
			$('#pq' + apq_id).html(apq_temp);
		}
		apq_user = user_name;
		apq_id = mmid;
		apq_temp = $('#pq' + apq_id).html();
		$('#pq' + apq_id).html('<img src="engine/skins/images/loading.gif">&#160;<a href="#">' + apq['Loading'] + '</a>');

		if (apq['Flag'] == 'Topic')
		{
			var values = {
				action: 'quote',
				id: mmid
			};
		}
		else if (apq['Flag'] == 'PM')
		{
			var values = {
				action: 'pmquote',
				id: mmid
			};
		}
		else
		{
			var values = new Array(1);
		}
		$.post('pjq.php?' + mmid, values, function(data) {apq_ready(data)});
		return false;
	}
	else
	{
		alert(apq['Must']);
	}
}

function apq_ready(data)
{
	if (apq_id != -1)
	{
		$('#pq' + apq_id).html(apq_temp);
		apq_id = -1;
	}
	var quote_message = match(data, 'quote_post');
	if (quote_message != '')
	{
		var endq = '[quote=' + apq_user + ']\n' + quote_message + '\n[/quote]\n';
		insert_text('',endq);
		return 1;
	}
	alert(data);
}

function match(str, substr)
{
	if (str.indexOf('<' + substr + '>') != -1)
	{
		var newstr = str.substring(str.indexOf('<' + substr + '>') + substr.length+2);
		newstr = newstr.substring(0, newstr.indexOf('</' + substr + '>'));
		return newstr;
	}
	else
		return '';
}

function getposOffset(overlay, offsettype)
{
//	if (document.selection && document.selection.createRange)
//	{
//		var box = overlay.getBoundingClientRect();
//		var body = document.body;
//		var docElem = document.documentElement;
//
//		var scrollTop = window.pageYOffset || docElem.scrollTop || body.scrollTop;
//		var scrollLeft = window.pageXOffset || docElem.scrollLeft || body.scrollLeft;
//		var clientTop = docElem.clientTop || body.clientTop || 0;
//		var clientLeft = docElem.clientLeft || body.clientLeft || 0;
//
//		var top  = box.top +  scrollTop - clientTop;
//		var left = box.left + scrollLeft - clientLeft;
//
//		return (offsettype=='left')? Math.round(left) : Math.round(top);
//	} else
//	{
		var totaloffset=(offsettype=='left')? overlay.offsetLeft : overlay.offsetTop;
		var parentEl=overlay.offsetParent;
		totaloffset=(offsettype=='left')? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
		return totaloffset;
//	}
}

function overlay(curobj, subobjstr, opt_position)
{
	if (document.getElementById) {
		var subobj=document.getElementById(subobjstr);
		if (subobjstr == 'bbcode_smileys' && bbcode_sm_vis) {
			bbcode_sm_vis = false;
			if (subobj) subobj.innerHTML = SmileysMapBB();
		}
		if (subobjstr == 'bbcode_color_map' && bbcode_cr_vis) {
			bbcode_cr_vis = false;
			if (subobj) subobj.innerHTML = ColorMapBB();
		}
		subobj.style.display=(subobj.style.display!='block')? 'block' : 'none';
		var xpos=getposOffset(curobj, 'left')+((typeof opt_position!='undefined' && opt_position.indexOf('right')!=-1)? -(subobj.offsetWidth-curobj.offsetWidth)/2 : 0); 
		var ypos=getposOffset(curobj, 'top')+((typeof opt_position!='undefined' && opt_position.indexOf('bottom')!=-1)? curobj.offsetHeight : 0);
		subobj.style.left=xpos+'px';
		subobj.style.top=ypos+'px';
		return false;
	}
	else
		return true;
}

function overlayclose(subobj)
{
	document.getElementById(subobj).style.display='none';
}

function showMapColor(color)
{
	document.getElementById("selectedMapColor").style.backgroundColor = color;
	document.getElementById("selectedMapColorBox").value = color;
}

function ColorMapBB()
{
	var colors = new Array(
		"#01669a","#000000","#000033","#000066","#000099","#0000cc","#0000ff","#330000","#330033",
		"#330066","#330099","#3300cc","#3300ff","#660000","#660033","#660066","#660099",
		"#6600cc","#6600ff","#990000","#990033","#990066","#990099","#9900cc","#9900ff",
		"#cc0000","#cc0033","#cc0066","#cc0099","#cc00cc","#cc00ff","#ff0000","#ff0033",
		"#ff0066","#ff0099","#ff00cc","#ff00ff","#003300","#003333","#003366","#003399",
		"#0033cc","#0033ff","#333300","#333333","#333366","#333399","#3333cc","#3333ff",
		"#663300","#663333","#663366","#663399","#6633cc","#6633ff","#993300","#993333",
		"#993366","#993399","#9933cc","#9933ff","#cc3300","#cc3333","#cc3366","#cc3399",
		"#cc33cc","#cc33ff","#ff3300","#ff3333","#ff3366","#ff3399","#ff33cc","#ff33ff",
		"#006600","#006633","#006666","#006699","#0066cc","#0066ff","#336600","#336633",
		"#336666","#336699","#3366cc","#3366ff","#666600","#666633","#666666","#666699",
		"#6666cc","#6666ff","#996600","#996633","#996666","#996699","#9966cc","#9966ff",
		"#cc6600","#cc6633","#cc6666","#cc6699","#cc66cc","#cc66ff","#ff6600","#ff6633",
		"#ff6666","#ff6699","#ff66cc","#ff66ff","#009900","#009933","#009966","#009999",
		"#0099cc","#0099ff","#339900","#339933","#339966","#339999","#3399cc","#3399ff",
		"#669900","#669933","#669966","#669999","#6699cc","#6699ff","#999900","#999933",
		"#999966","#999999","#9999cc","#9999ff","#cc9900","#cc9933","#cc9966","#cc9999",
		"#cc99cc","#cc99ff","#ff9900","#ff9933","#ff9966","#ff9999","#ff99cc","#ff99ff",
		"#00cc00","#00cc33","#00cc66","#00cc99","#00cccc","#00ccff","#33cc00","#33cc33",
		"#33cc66","#33cc99","#33cccc","#33ccff","#66cc00","#66cc33","#66cc66","#66cc99",
		"#66cccc","#66ccff","#99cc00","#99cc33","#99cc66","#99cc99","#99cccc","#99ccff",
		"#cccc00","#cccc33","#cccc66","#cccc99","#cccccc","#ccccff","#ffcc00","#ffcc33",
		"#ffcc66","#ffcc99","#ffcccc","#ffccff","#00ff00","#00ff33","#00ff66","#00ff99",
		"#00ffcc","#00ffff","#33ff00","#33ff33","#33ff66","#33ff99","#33ffcc","#33ffff",
		"#66ff00","#66ff33","#66ff66","#66ff99","#66ffcc","#66ffff","#99ff00","#99ff33",
		"#99ff66","#99ff99","#99ffcc","#99ffff","#ccff00","#ccff33","#ccff66","#ccff99",
		"#ccffcc","#ccffff","#ffff00","#ffff33","#ffff66","#ffff99","#ffffcc","#ffffff"
	);
	var html = '<table class="tbl"><tr>';
	for (var i=0; i<colors.length; i++) {
		html += "<td style='background-color:" + colors[i] + "' onclick=\"insert_text('[color=" + colors[i] + "]', '[/color]');\" onfocus=\"showMapColor('" + colors[i] +  "');\" onmouseover=\"showMapColor('" + colors[i] + "');\">"
		html += '</td>';
		if ((i+1) % 18 == 0)	html += '</tr><tr>';
	}
	html += '<tr><td colspan="9" id="selectedMapColor" height="16"></td>'
	+ '<td colspan="9">'
	+ '<input id="selectedMapColorBox" name="selectedMapColorBox" type="text" size="7" maxlength="7" style="text-align:center;font-weight:bold;width:95%;border:1px solid;" value="" />'
	+ '</td></tr></table>'
	return html;
}

function tag_url()
{
    var FoundErrors = '';
	var thesel ='';
	if ( (ua_vers >= 4) && is_ie && is_win)
	{
	thesel = document.selection.createRange().text;
	} else thesel ='My Webpage';

    if (!thesel) {
        thesel ='My Webpage';
    }

    var enterURL   = prompt(text_enter_url, "http://");
    var enterTITLE = prompt(text_enter_url_name, thesel);

    if (!enterURL) {
        FoundErrors += " " + error_no_url;
    }
    if (!enterTITLE) {
        FoundErrors += " " + error_no_title;
    }

    if (FoundErrors) {
        alert("Error!"+FoundErrors);
        return;
    }

	doInsert("[url="+enterURL+"]"+enterTITLE+"[/url]", "", false);
};

function SmileysMapBB()
{
	var html = "";
	for (var i=0; i<bbcode_sm_img.length; i++) {
		html += "<img src=\"engine/skins/emoticons/" + bbcode_sm_img[i] + "\" alt=\"" + bbcode_sm_txt[i] + "\" onclick=\"return insert_text('',' " + bbcode_sm_txt[i] + " ');\" />"
	}
	return html;
}

var quote_text = '';
var apq;
var apq_user;
var apq_id = -1;
var apq_temp;
